using com.tc.zigbee.entity;
using com.tc.zigbee.entity.device;
using com.tc.zigbee.mt;
using com.tc.zigbee.zcl;
using com.tc.zigbee.zcl.command;
using com.tc.util;
using com.tc.zigbee.mt.request;
using com.tc.gateway.device.ha;
namespace com.tc.zigbee.entity.device.ha
{
    public class ZigbeePressureSensor : ZigbeeDevice, PressureSensor
    {

        public ZigbeePressureSensor(Endpoint endpoint, ZigbeeManager zm)
            : base(endpoint, zm) 
        {

        }

        override public void action()
        {


        }

        public int getPressure()
        {
            DataValue dv = this.endpoint.getAttribute(ZclConstants.ZCL_CLUSTER_ID_MS_PRESSURE_MEASUREMENT, (short)0x0000);
            if (dv == null)
                return 0;
            return DataUtil.shortValue(dv.value);
        }

        public void receive()
        {
            MtCommand mc = RequestFactory.MtBindRequest(
                    getEndpoint().getParent().getNwkAddress(),
                    DataUtil.longValue(this.getEndpoint().getParent().getIeeeAddress()),
                    getEndpoint().getEndpointId(),
                    ZclConstants.ZCL_CLUSTER_ID_MS_PRESSURE_MEASUREMENT,
                    DataUtil.longValue(manager.coordinator().getIeeeAddress()),
                    manager.getRemoteControl().getEndpointId()
                    );
            RequestQueue.offer(mc);

        }

        public void config()
        {
            ReportConfig c = new ReportConfig();
            ReporterConfigurationRecord rec = new ReporterConfigurationRecord();
            rec.attribute = 0x0000;
            rec.datatype = ZclConstants.ZCL_DATATYPE_UINT16;
            rec.maxInterval = -1;
            rec.minInterval = 0x0000;
            rec.reportableChange = new DataValue(ZclConstants.ZCL_DATATYPE_UINT16, DataUtil.bytesValue((short)12));
            c.getRecords().Add(rec);

            byte[] zclcommand = ZclCommandCreator.reportConfig((byte)0, c);
            MtCommand mc = RequestFactory.MtAfDataRequest((byte)2,
                    getEndpoint().getParent().getNwkAddress(),
                //(short)1,
                    getEndpoint().getEndpointId(),
                    (byte)manager.getRemoteControl().getEndpointId(),
                    ZclConstants.ZCL_CLUSTER_ID_MS_PRESSURE_MEASUREMENT,
                    (byte)0,
                    (byte)0,
                    (byte)2,
                    (byte)zclcommand.Length,
                    zclcommand
                    );
            RequestQueue.offer(mc);

        }
    }
}